package com.hyts.serialize.json.gson;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.hyts.serialize.json.JsonSerializeHandler;

/**
 * project-name:coffice-toolbox
 * package-name:com.hyts.serialize.json.gson
 * author:Libo/Alex
 * create-date:2019-06-28 11:00
 * email:liboware@gmail.com
 * description:此类主要用于:
 */
public class GsonSerializeHandler implements JsonSerializeHandler<JsonElement> {


    private static Gson gson = new GsonBuilder().create();


    @Override
    public <P> JsonElement buildJSONObject(P param) {
        return new GsonBuilder().create().toJsonTree(param);
    }

    @Override
    public <P> String buildJSONString(P param) {
        return gson.toJson(param);
    }

    @Override
    public <P> JsonElement buildJSONObject(String jsonStr) {
        return new GsonBuilder().create().toJsonTree(jsonStr);
    }

    @Override
    public <P> P buildBean(String jsonStr, Class<P> clazzType) {
        return gson.fromJson(jsonStr,clazzType);
    }

    @Override
    public Object buildBean(String jsonStr) {
        return gson.fromJson(jsonStr,Object.class);
    }
}
